/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DTO.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author QuangHau
 */
public class ChuDeDAO {

    public static ArrayList<ChuDeDTO> selectAll() {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectDanhSachChuDe_All}");
                ResultSet rs = cs.executeQuery();
                ArrayList<ChuDeDTO> arr = new ArrayList<ChuDeDTO>();
                while (rs.next()) {
                    ChuDeDTO item = new ChuDeDTO();
                    item.setMaChuDe(rs.getInt("machude"));
                    item.setTenChuDe(rs.getString("tenchude"));
                    item.setLinkChuDe(rs.getString("linkchude"));
                    item.setNgayBatDau(new java.util.Date(rs.getTimestamp("ngaybatdau").getTime()));
                    item.setMaWebsite(rs.getInt("mawebsite"));
                    item.setTenWebsite(rs.getString("tenwebsite"));
                    arr.add(item);
                }
                con.close();
                return arr;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static ArrayList<ChuDeDTO> selectByWebsite(int maWebsite) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectDanhSachChuDe_All(?)}");
                cs.setInt("mawebsite", maWebsite);
                ResultSet rs = cs.executeQuery();
                ArrayList<ChuDeDTO> arr = new ArrayList<ChuDeDTO>();
                while (rs.next()) {
                    ChuDeDTO item = new ChuDeDTO();
                    item.setMaChuDe(rs.getInt("machude"));
                    item.setTenChuDe(rs.getString("tenchude"));
                    item.setLinkChuDe(rs.getString("linkchude"));
                    item.setNgayBatDau(new java.util.Date(rs.getTimestamp("ngaybatdau").getTime()));
                    item.setMaWebsite(rs.getInt("mawebsite"));
                    item.setTenWebsite(rs.getString("tenwebsite"));
                    arr.add(item);
                }
                con.close();
                return arr;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static ChuDeDTO ThongTinChuDe(int maChude) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_SelectDanhSachChuDe_All(?,?)}");
                cs.setObject("mawebsite", null);
                cs.setInt("machude", maChude);
                ResultSet rs = cs.executeQuery();
                ChuDeDTO chudeDTO = new ChuDeDTO();
                if (rs.next()) {
                    chudeDTO.setMaChuDe(rs.getInt("machude"));
                    chudeDTO.setTenChuDe(rs.getString("tenchude"));
                    chudeDTO.setLinkChuDe(rs.getString("linkchude"));
                    chudeDTO.setNgayBatDau(new java.util.Date(rs.getTimestamp("ngaybatdau").getTime()));
                    chudeDTO.setMaWebsite(rs.getInt("mawebsite"));
                    chudeDTO.setTenWebsite(rs.getString("tenwebsite"));
                }
                con.close();
                return chudeDTO;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return null;
    }

    public static boolean insertChuDe(ChuDeDTO chudeDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_InsertChuDe(?,?,?,?)}");
                cs.setString("tenchude", chudeDTO.getTenChuDe());
                cs.setString("linkchude", chudeDTO.getLinkChuDe());
                cs.setInt("mawebsite", chudeDTO.getMaWebsite());
                cs.setTimestamp("ngaybatdau", new java.sql.Timestamp(chudeDTO.getNgayBatDau().getTime()));
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static boolean updateChuDe(ChuDeDTO chudeDTO) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_UpdateChuDe(?,?,?,?,?)}");
                cs.setInt("machude", chudeDTO.getMaChuDe());
                cs.setString("tenchude", chudeDTO.getTenChuDe());
                cs.setString("linkchude", chudeDTO.getLinkChuDe());
                cs.setInt("mawebsite", chudeDTO.getMaWebsite());
                cs.setTimestamp("ngaybatdau", new java.sql.Timestamp(chudeDTO.getNgayBatDau().getTime()));
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static boolean deleteChuDe(int maChuDe) {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                CallableStatement cs = con.prepareCall("{call usp_DeleteChuDe(?)}");
                cs.setInt("machude", maChuDe);
                int rs = cs.executeUpdate();
                if (rs > 0) {
                    return true;
                }
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
        }
        return false;
    }

    public static int maxID() {
        Connection con = DataProvider.Connection();
        if (con != null) {
            try {
                PreparedStatement ps;
                ps = con.prepareCall("select max(machude) as maxid from chude");
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    return rs.getInt("maxid");
                }
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(ChuDeDAO.class.getName()).log(Level.SEVERE, null, ex);
                return -1;
            }
        }
        return -1;
    }
//    public static void main (String[] arg)
//    {
//        ChuDeDTO t = new ChuDeDTO();
//        t.setLinkChuDe("hhh");
//        t.setMaWebsite(1);
//        t.setTenChuDe("jhghj");
//        t.setNgayBatDau(new Date(System.currentTimeMillis()));
//        insertChuDe(t);
//       //System.out.println(selectByWebsite(1).size());
//    }
}
